package com.team407.cheyoubang.manager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.team407.cheyoubang.bean.LoginBeanReq;
import com.team407.cheyoubang.bean.RequestBean;
import com.team407.cheyoubang.callback.NetCallBack;
import com.team407.cheyoubang.net.Com;
import com.team407.cheyoubang.parse.LoginParseData;
import com.team407.cheyoubang.parse.ParseData;
import com.team407.cheyoubang.utils.Constant;


public class RequestManager {
	private static RequestManager manager;
	private ExecutorService service = Executors.newFixedThreadPool(5);
	private Com com;
	private RequestManager(){
		com = new Com();
	}
	
	public static RequestManager newInstance(){
		if(manager == null){
			manager = new RequestManager();
		}
		return manager;
	}
	
	/**
	 * 登录
	 * @param memberNo
	 * @param password
	 * @param callBack
	 */
	public void loginComm(String memberNo, String password, NetCallBack callBack){
		//创建请求数据
		LoginBeanReq req = new LoginBeanReq(Constant.Url.LOGIN_URL, 9, memberNo, password);
		//创建解释器
		LoginParseData parseData = new LoginParseData();
		//执行请求
		doCom(parseData, req, callBack);
	}
	
	//异步执行请求操作
	private void doCom(ParseData parseData, RequestBean requestBean, NetCallBack netCallBack){
		service.execute(new RequestJsonRunnable(parseData, requestBean, netCallBack));
	}
	
	
	private class RequestJsonRunnable implements Runnable{
		private ParseData mParseData;
		private RequestBean mRequestBean;
		private NetCallBack mNetCallBack;
		public RequestJsonRunnable(ParseData parseData, RequestBean requestBean, NetCallBack netCallBack){
			mParseData = parseData;
			mRequestBean = requestBean;
			mNetCallBack = netCallBack;
		}
		@Override
		public void run() {
			com.doJsonPost(mRequestBean, mParseData, mNetCallBack);
		}
		
	}
	
}
